package kr.co.shoppingmall.member;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/member")
public class MemberController {
	
	@Resource
	private MemberService memberService;
	
	@RequestMapping("/joinform.htm")
	public String joinform(){
		return "/member/joinform";
	}
	
	@RequestMapping("/join.htm")
	public String join(MemberDTO dto) {
		memberService.insertMember(dto);
		return "redirect:/index.htm";
	}
	
	@RequestMapping("/login.htm")
	public String login(MemberDTO dto, HttpServletRequest request,Model model) {
		int ok = memberService.getLoginCheck(dto);
		
		if(ok==1){
			HttpSession session = request.getSession();
			session.setAttribute("id", dto.getMem_id());
			return "redirect:/index.htm";
		}else{
			return "/member/fail";
		}
	}

	@RequestMapping("/logout.htm")
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:/index.htm";
	}
	
	@RequestMapping("/updateform.htm")
	public String updateform(HttpSession session, Model model) {
		String id = (String) session.getAttribute("id");
		MemberDTO dto = memberService.getMember(id);
		model.addAttribute("mem", dto);
		return "/member/updateform";
	}

	@RequestMapping("/update.htm")
	public String update(MemberDTO dto) {
		memberService.updateMember(dto);
		return "redirect:/index.htm";
	}

	@RequestMapping("/deleteform.htm")
	public String deleteform() {
		return "/member/deleteform";
	}

	@RequestMapping("/delete.htm")
	public String delete(MemberDTO dto, HttpSession session) {
		String id = (String) session.getAttribute("id");
		dto.setMem_id(id);
		memberService.deleteMember(dto);
		session.invalidate();
		return "redirect:/index.htm";
	}

}
